package cate.game.activity.customwarorder;

import cate.common.table.activity.customwarorder.row.CustomWarorderBaseRow;
import cate.common.table.activity.customwarorder.row.CustomWarorderChooseRow;
import cate.common.util.GameResult;
import cate.game.activity.base.core.SubActivitySingle;
import cate.game.activity.customwarorder.msg.CustomWarOrderChooseResp;
import easy.java.dev.note.NoteField;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class RoleCustomWarOrderChoose extends SubActivitySingle<RoleCustomWarOrder> {
    @NoteField("锁定的tid 可能不止一个")
    public List<Integer> chooseItems;

    @Override
    public void initialize(RoleCustomWarOrder parent) {
        super.initialize(parent);
        if (chooseItems == null) {
            chooseItems = new ArrayList<>();
        }
    }

    public GameResult<Void> choose(int tid) {
        GameResult<Void> r = new GameResult<>();
        if (!chooseItems.isEmpty()) {
            return r.fail("您已经自选过了噢");
        }
        CustomWarorderChooseRow chooseRow = role.getGame().table.customWarorder.choose.get(tid);
        if (chooseRow == null) {
            return r.fail("自选奖励有误");
        }
        CustomWarorderBaseRow baseRow = role.getGame().table.customWarorder.base.get(parent.configTid());
        if (baseRow == null) {
            return r.fail("活动不存在");
        }
        if (baseRow.chooseGroup != chooseRow.group) {
            return r.fail("自选奖励有误");
        }
        chooseItems.add(tid);
        noticeUpdate();
        return r;
    }

    public void noticeUpdate() {
        role.sendNow(new CustomWarOrderChooseResp(funcId(), this));
    }

    public void excelCheck() {

    }

    public void beforeOver() {

    }

    public void clear() {
        chooseItems.clear();
    }

    public boolean hasChoosed() {
        return CollectionUtils.isNotEmpty(chooseItems);
    }
}
